package cn.doitedu;

import cn.doitedu.core.DataManagerService;
import cn.doitedu.core.StudentBean;

import java.util.List;
import java.util.Scanner;

public class DataManagerCommandLine {

    public static void main(String[] args) throws Exception {

        DataManagerService dataManagerService = new DataManagerService(args[0],args[1]);

        Scanner scanner = new Scanner(System.in);


        System.out.println("******************");
        System.out.println("1.根据id查询");
        System.out.println("2.根据name查询");
        System.out.println("3.根据兴趣词查询");
        System.out.println("4.录入新数据");
        System.out.println("5.退出系统");
        System.out.println("******************");
        System.out.print("请选择功能: ");

        while(scanner.hasNext()){

            String s = scanner.nextLine();
            // TODO 根据用户选择的菜单，去执行相应的方法，并打印结果到控制台
            switch (s) {
                case "1":
                    System.out.print("请输入id:");
                    String queryId = scanner.nextLine();
                    String studentById = dataManagerService.findStudentById(Integer.parseInt(queryId));
                    System.out.println(studentById);
                    break;
                case "2":
                    System.out.print("请输入name:");
                    String queryName = scanner.nextLine();
                    List<String> studentsByName = dataManagerService.findStudentsByName(queryName);
                    for (String stu : studentsByName) {
                        System.out.println(stu);
                    }
                    break;
                case "3":
                    System.out.print("请输入兴趣词:");
                    String queryWord = scanner.nextLine();
                    List<String> studentsByWord = dataManagerService.findStudentsByInterestWord(queryWord);
                    for (String stu : studentsByWord) {
                        System.out.println(stu);
                    }
                    break;
                case "4":
                    System.out.print("请输入学员信息[各字段用逗号分隔]:");
                    String stuInfo = scanner.nextLine();
                    String[] arr = stuInfo.split(",");
                    StudentBean bean = new StudentBean(Integer.parseInt(arr[0]), arr[1], Integer.parseInt(arr[2]), arr[3], arr[4], arr[5]);
                    dataManagerService.appendStudent(bean);
                    System.out.println("录入成功");
                    break;
                case "5":
                    System.out.print("正在退出.......");
                    // 调用service做元数据持久化
                    dataManagerService.exit();
                    // 退出主程序
                    System.exit(0);

                    break;

                // TODO 添加系统退出菜单
                //  退出前，需要把 兴趣词索引  持久化保存起来，下次启动时直接加载到内存，这样就不用每次启动都对数据重建索引了
            }

            // 提供交互式命令行界面
            System.out.println("******************");
            System.out.println("1.根据id查询");
            System.out.println("2.根据name查询");
            System.out.println("3.根据兴趣词查询");
            System.out.println("4.录入新数据");
            System.out.println("5.退出系统");
            System.out.println("******************");
            System.out.print("请选择功能:");

        }




    }

}
